package cn.doitedu.datayi.data_service.dao;

import cn.doitedu.datayi.data_service.vo.FunnelDefineVo;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 平台元数据库中，漏斗定义信息表，建表语句：
 * CREATE TABLE `platform_meta_funnel_def` (
 *   `id` int(11) NOT NULL AUTO_INCREMENT,
 *   `funnel_name` varchar(255) DEFAULT NULL,
 *   `definition_json` varchar(255) DEFAULT NULL,
 *   PRIMARY KEY (`id`)
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 *
 */

@Repository
public class PlatformMetaDaoImpl implements PlatformMetaDao {
    @Override
    public boolean saveFunnelDefinition(FunnelDefineVo vo) throws Exception {

        boolean flag = true;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/realtimedw?serverTimezone=Asia/Shanghai", "root", "123456");
            PreparedStatement stmt = conn.prepareStatement("insert into platform_meta_funnel_def (funnel_name,definition_json) values(?,?)");
            stmt.setString(1, vo.getFunnelName());

            ObjectMapper obj = new ObjectMapper();
            stmt.setString(2, obj.writeValueAsString(vo));

            stmt.execute();
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    @Override
    public FunnelDefineVo getFunnelDefinitionByName(String funnelName) {

        return null;
    }
}
